#!/bin/bash
if grep -Eqi "16.10" /etc/*-release || echo "${Ubuntu_Version}" | grep -Eqi '^16.10'; then
        Ros_ver='kinetic'
elif grep -Eqi "16.04" /etc/*-release || echo "${Ubuntu_Version}" | grep -Eqi '^16.04'; then
        Ros_ver='kinetic'
elif grep -Eqi "18.04" /etc/*-release || echo "${Ubuntu_Version}" | grep -Eqi '^18.04'; then
        Ros_ver='melodic'
elif grep -Eqi "18.10" /etc/*-release || echo "${Ubuntu_Version}" | grep -Eqi '^18.10'; then
        Ros_ver='melodic'
fi
golo_ros_ver=$(cat /opt/openresty/nginx/scripts/golo.conf |grep ROS_PATH |awk -F "/" '{print $4}')
sudo sed -i "s/$golo_ros_ver/$Ros_ver/" /opt/openresty/nginx/scripts/golo.conf

ip=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v 172.17.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"|awk 'NR==1'`
byip=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v 172.17.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"|cut -d \. -f 1-3|awk 'NR==1'`
if [ "$ip" = "" ]; then
	     exit 1
     fi
nuchost=$(cat /etc/hosts |grep 127.0.1.1 |awk '{print $2}')
nucdns=$(cat /etc/resolv.conf |grep nameserver |awk '{print $2}')
golo_ver=$( cat /opt/openresty/nginx/scripts/golo.conf |grep GOLO_Ver |tr -d "GOLO_Ver=") 
golo_ver=$(echo $golo_ver|sed 'N;s/\n//g')    
golo_ip=$( cat /opt/openresty/nginx/scripts/golo.conf |grep ROSIP |tr -d "ROSIP="|awk -F "[\'\']" '{print $2}')
golo_cid=$( cat /opt/openresty/nginx/scripts/golo.conf |grep cid |tr -d "cid=")
rtk_ip=$( cat /opt/openresty/nginx/scripts/golo.conf |grep RTKIP |tr -d "RTKIP="|awk -F "[\'\']" '{print $2}'|cut -d \. -f 1-3)
htmlip=$( cat /opt/openresty/nginx/html/t.html |grep "var ip2"|awk -F "=" '{print $2}'|awk -F "[\"\"]" '{print $2}')
websocket_cid=$( cat /home/nuc/golo_ws/src/golo/websocket-client/scripts/config.py |grep cid |tr -d "cid=" |awk '{print $1}')
nuc_user=$(env | grep ^HOME= | cut -c 6-)
sudo sed -i "s#/home/nuc#$nuc_user#g" /opt/openresty/nginx/scripts/golo.conf
sudo sed -i "s#$golo_ip#$ip#g" /opt/openresty/nginx/scripts/golo.conf
sudo sed -i "s#$rtk_ip#$byip#g" /opt/openresty/nginx/scripts/golo.conf
sudo sed -i "s#$htmlip#$ip#g" /opt/openresty/nginx/html/t.html
sudo sed -i "s#$golo_cid#$websocket_cid#g" /opt/openresty/nginx/scripts/golo.conf
sudo sed -i "s/$nuchost/$ip/" /etc/hosts
sudo sed -i "1c $ip" /etc/hostname
sudo sed -i "s/$nucdns/8.8.8.8/" /etc/resolv.conf
sudo sed -i "/8.8.8.8/anameserver 114.114.114.114" /etc/resolv.conf
